//
//  QTLoginViewController.m
//  qdqts
//
//  Created by jonn on 14-6-21.
//  Copyright (c) 2014年 seas.cheng. All rights reserved.
//

#import "QTLoginViewController.h"
#import "QTClient.h"
#import "QTUserDefault.h"
#import "QTUserInfoViewController.h"
#import "Global.h"

@interface QTLoginViewController ()

@end

@implementation QTLoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.hidesBackButton = YES;
    self.title=@"登录";
    selectedViewController=self.navigationController;
    
//    //UIView设置阴影
    [_inputView.layer setShadowOffset:CGSizeMake(0.5, 0.5)];
    [_inputView.layer setShadowRadius:2];
    [_inputView.layer setShadowOpacity:0.1];
    [_inputView.layer setShadowColor:[UIColor blackColor].CGColor];
    //UIView设置边框
    [_inputView.layer setCornerRadius:5];
    [_inputView.layer setBorderWidth:0.5];
    [_inputView.layer setBorderColor:GRAYCOLOR.CGColor];
    
    
    //Button设置圆角
    [_loginButton.layer setMasksToBounds:YES];
    [_loginButton.layer setCornerRadius:3];
    
    
    
    //[self readNSUserDefaults];
    QTUserDefault *userDefault=[QTUserDefault sharedInstance];
    self.userName.text=[userDefault readNSUserDefaults:@"userName"];
    self.passwd.text=[userDefault readNSUserDefaults:@"passwd"];
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated{
    [self.hidenTabBarDelegate hideTabBar:YES];
}

-(void)viewWillDisappear:(BOOL)animated{
    [self.hidenTabBarDelegate hideTabBar:NO];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)login:(id)sender {
    //mainPageViewController *vc = [[[mainPageViewController alloc]init]autorelease];
    
    [(UIButton *)sender setEnabled:NO];
    if ([_userName.text isEqual:@""]) {
        [self showAlert:@"用户名不能为空！"];
    }else if ([_passwd.text isEqual:@""]){
        [self showAlert:@"密码不能为空！"];
    }else{
        [_refreshSpinner startAnimating];
        [[QSClient instance] login:_userName.text loginPassword:_passwd.text completionBlock:^(User *user, NSError *error){
            NSLog(@"%@",user);
            
            if (error) {
                [_refreshSpinner stopAnimating];
                [self showAlert:@"服务器连接失败，请检查网络"];
            }else if ([user.state isEqual:@"false"]) {
                [_refreshSpinner stopAnimating];
                [self showAlert:user.noticevalue];
            }else if ([user.state isEqual:@"true"]) {
                [_refreshSpinner stopAnimating];
                [_userName resignFirstResponder];
                [_passwd resignFirstResponder];
                QTUserInfoViewController *userInfoController = [[QTUserInfoViewController alloc] init];
                QTUserDefault *userDefault=[QTUserDefault sharedInstance];
                userInfoController.puid = user.puid;
                NSLog(@"puid is %@",user.puid);
                [userDefault saveNSUserDefaults:self.userName.text forKey:@"userName"];
                [userDefault saveNSUserDefaults:self.passwd.text forKey:@"passwd"];
                [userDefault saveNSUserDefaults:user.puid forKey:@"puid"];
//                userInfoController.view.frame=CGRectMake(0, 200, 500, 500);
//                [self presentViewController:userInfoController animated:YES completion:nil];
                [self.navigationController pushViewController:userInfoController animated:NO];
            }
        }];
    }
    
}


-(void)showAlert:(NSString *)msg
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示"
                                                  message:msg
                                                 delegate:self
                                        cancelButtonTitle:@"确定"
                                        otherButtonTitles:nil, nil];
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0) {
        [self.loginButton setEnabled:YES];
        
    }
    
}

//-(void)saveNSUserDefaults
//{
//    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
//    [userDefaults setObject:self.userName.text forKey:@"count"];
//    [userDefaults setObject:self.passwd.text forKey:@"passwd"];
//    [userDefaults synchronize];
//}
//
//-(void)readNSUserDefaults
//{
//    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
//    self.userName.text=[userDefaults stringForKey:@"count"];
//    self.passwd.text=[userDefaults stringForKey:@"passwd"];
//}

@end
